package com.itheima.reggie;

import com.google.zxing.WriterException;
import com.itheima.reggie.QRCodeGenerator;
import com.itheima.reggie.utils.SendMessage;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@SpringBootTest
public class test {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    void getURL() throws IOException {

    }
    @Test
    void getQRCode() throws IOException {
        File directory = new File("");//参数为空
        String courseFile = directory.getCanonicalPath() ;
        System.out.println(courseFile);
        //定义扫描二维码要跳转的链接地址，这里以百度为例
        String urlPath = "https://www.baidu.com";
//定义二维码图片名称
        String fileName = "baidu.png";
//存放二维码路径
        String savePath = courseFile+"\\src\\main\\resources\\backend\\images\\QRCode";
        try {
            //调用生成二维码方法前需要判断存放二维码的路径是否存在，不存在则新建，如果路径不存在直接在该路径下生成二维码会报错
            File f = new File(savePath);
            if (!f.exists()) {
                f.mkdirs();
            }
            File file = new File(savePath,fileName);
            if(!file.exists()){
                //生成二维码方法
                QRCodeGenerator.generateQRCodeImage(urlPath, 350, 350, savePath+"\\" +fileName);
            }
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testList()
    {
        System.out.println();
        ListOperations<String,String> listOperations = redisTemplate.opsForList();
        List<String> mylist = listOperations.range("mylist", 0, -1);
        if(mylist!=null)
        {
            mylist.forEach(s-> System.out.println(s));
        }
        List<String> list=new ArrayList<>();
        Collections.addAll(list,"123","456","789");
        listOperations.leftPushAll("mylist",list);
    }
    @Test
    public void testSendMessage()
    {

    }
}
